/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package in.co.sneh.controller.FileUploadServlet;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.*;

@WebServlet(name = "FileUploadServlet", urlPatterns = {"/FileUploadServlet"})     // specify urlPattern for servlet
@MultipartConfig                                               // specifies servlet takes multipart/form-data
public class FileUploadServlet extends HttpServlet {

    String outfile;
    conexion con = new conexion();
       public FileUploadServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {}
       
       
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        FileItemFactory factory = new DiskFileItemFactory();
        ServletFileUpload upload = new ServletFileUpload(factory);

        try {

            // get access to file that is uploaded from client
            Part p1 = request.getPart("file");
            Scanner s2 = new Scanner(p1.getInputStream());
            String filename2 = s2.nextLine();
            InputStream is = p1.getInputStream();

            String outputfile = this.getServletContext().getRealPath(p1.getSubmittedFileName());  // get path on the 
            outfile = outputfile;
            FileOutputStream os = new FileOutputStream(outputfile);
            int ch = is.read();
            while (ch != -1) {
                os.write(ch);
                ch = is.read();
            }

            String ruta_archivo_expediente = outfile;

            // TODO code application logic here
            out.println("<HTML>");
            out.println("<head>");
            out.println("  <meta http-equiv=" + "'Content-Type'" + " content=" + "'text/html; charset=UTF-8'" + ">");
            out.println(" <title> Latam Airlines Group - Resultados</title>");
            out.println("  <meta name=" + "'description'" + " content=" + "'Latam Airlines Group Home'" + ">");
            out.println(" <meta name=" + "'keywords'" + "content=" + "'Latam Airlines Group, Home'" + ">");
            out.println("<link rel=" + "'stylesheet'" + "type=" + "'text/css'" + " href=" + "'css/font.css'" + ">");
            out.println(" <link rel=" + "'stylesheet'" + "type=" + "'text/css'" + "href=" + "'css/style.css'" + ">");
            out.println(" <link rel=" + "'stylesheet'" + "type=" + "'text/css'" + "href=" + "'css/skin.css'" + ">");
            out.println(" <link href=" + "'css/jquery.lightbox-0.5.css'" + " rel=" + "'stylesheet'" + " type=" + "'text/css'" + ">");
             out.println("   <link href='http://www.lan.com/images/sitio_personas/layout/lancom_ico.png' rel='shortcut icon' type='image/x-icon'>");
            out.println(" <style type=" + "'text/css'" + "></style>");
            out.println("</head>");
            out.println("<BODY>");
            out.println(" <div id=" + "'mboxScriptContainer'" + "style=" + "'display:none;visibility:hidden;'" + "> </div> ");
            out.println(" <style>.mboxDefault{visibility:hidden;}</style>");
            out.println(" <div id=" + "'wrap'" + ">");
            out.println(" <div id=" + "'content-home'" + ">");
            out.println("<div class=" + "'header'" + ">");
            out.println(" <div class=" + "'language-box'" + ">");
            out.println("<div class=" + "'text-links-language'" + ">");
            out.println(" <ul>");
         
            out.println("      </ul>");
            out.println("  </div>");
            out.println(" </div>");
            out.println("           <div class=" + "'logos'" + ">");
            out.println("                   <div class=" + "'logo-page'" + ">");
            out.println("      </div>");
            out.println("            <div class=" + "'logo-latam'" + ">");
            out.println("           <img src=" + "'css/image/logo-bajada-ingles.jpg'" + "width=" + "'148'" + "height=" + "'40'" + " usemap=" + "'#logomap'" + ">");
            out.println("           <map name=" + "logomap" + ">");
            out.println("                   <area shape=" + "'rect'" + "coords=" + "'0,0,74,28'" + "href=" + "'http://www.lan.com/'" + "target=" + "'_blank'" + ">");
            out.println("                    <area shape=" + "'rect'" + " coords=" + "'74,0,148,28'" + " href=" + "'http://www.tam.com.br/'" + " target=" + "'_blank'" + ">");
            out.println("              </map>");
            out.println("       </div>");
             out.println(" <div class="+"'menu'" +"style="+"'width:621px;'"+">");
              out.println("                   <a href="+"'/VerificadorCampos/index.html'"+">");
              out.println("                       <div class="+"'button'"+"><p>");
              out.println("                           Retornar");
               out.println("                      </p></div>");
               out.println("                  </a>");
                out.println("                 <a href="+"'/VerificadorCampos/Documentacion.jsp'"+">");
                out.println("                     <div class="+"'button"+"'><p>");
                out.println("                         Documentacion");
                out.println("                     </p></div></a>");
                out.println("  <a href="+"/VerificadorCampos/Documentacion.jsp"+">");
                out.println("                    <div class="+"button"+"><p>");
                out.println("                        TEF");
                out.println("                    </p>");
                out.println("                    </div>");
                out.println("                </a>");
                out.println("             </div>");
            out.println("  </div>");
            out.println("  </div>");

            out.println("  <div id=" + "'homeLnkTop'" + "></div>");
            out.println("   <div class=" + "'none'" + ">");
            out.println("         <div class=" + "'content-top-right'" + ">");
            out.println("  <div class=" + "'content-top-right-bucket'" + "></div>");
            out.println("      </div>");
            out.println("       <div class=" + "'content-center'" + ">");
            out.println("                <div class=" + "'content-center-left'" + ">");
            out.println("                            <img src=" + "'css/image/image-home1.jpg'" + " border=" + "'0'" + ">");
            out.println("                    <div class=" + "'title-home'" + ">");
            out.println("                        <div class=" + "'phrase-1'" + ">");
            out.println("                        </div>");
            out.println("                    </div>");
            out.println("                </div>");
            out.println("                <div id=" + "'newsImg'" + ">");
            out.println("                    <div class=" + "'content-center-right'" + ">");
            out.println("<textarea ROWS=13 COLS=60>");
                out.println(" ================== CARGA DE ARCHIVOS TEF================== \n");
            
            
       
         //out.wait(20);
           if(outputfile.endsWith("csv")){
               
         InsercionBD ins = new InsercionBD();
            //   out.println(request.getServletContext());
            if (ins.insercionBD(ruta_archivo_expediente, out)) {                
                out.println(" =================== Archivos insertados ====================");
                con.borrarrepetidos();
                out.println("================ Archivos Repetidos borrados ===============");
                ExportacionBD export = new ExportacionBD();
                export.exportacionBD("C:\\GestionATO\\ReclamosEquipaje\\DatosCSV\\expedientes_tef");
                out.println("================ Informe semanal Exportado =================");
            }else
            {           
            }
           }
           else{out.println("Extension no permitida");}
            out.println("</textarea>");
            out.println("                    </div>");
            out.println("                </div>");
            out.println("            </div>");
            out.println("<br>");
            out.println("<br>");
            out.println("<br>");
            out.println("<br>");
            out.println("<br>");
            out.println("<br>");
            out.println("<br>");
            out.println("<br>");
            out.println("<br>");
            out.println("<br>");
            out.println("<br>");
            out.println("<br>");
            out.println("<br>");
            out.println("<br>");
            out.println("<br>");
            out.println("<br>");
            out.println("<br>");
            out.println("<br>");
            out.println("<br>");
            out.println("<br>");
            out.println("<br>");
            out.println("<br>");
            out.println("<br>");
            out.println("<br>");
            out.println("<br>");
           
            out.println("        </div>");
            
            out.println("    </div>");
            out.println("</div>");

            out.println("</BODY>");
            out.println("</HTML>");

            os.close();

        } catch (Exception ex) {
            out.println("Exception -->" + ex.getMessage());
        } finally {
            out.close();
        }

    }

} // end of UploadServlet
